package com.youtellv1.utils;

import com.youtellv1.entity.*;
import com.youtellv1.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class PageController {

    @Autowired
    private TeachersService teachersService;

    @Autowired
    private PostService postService;

    @Autowired
    private ChildService childService;

    @Autowired
    private ChildPayService childPayService;

    @Autowired
    private GSalService gsalService;

    @Autowired
    private YSalService ySalService;

    @GetMapping("/teacher/")
    public String teacherPage() {
        return "teachers";
    }

    @GetMapping("/add_teacher/")
    public String showAddTeacherPage() {
        return "addteacher";
    }

    @PostMapping("/add_teacher/")
    public String addTeacherForm(Teachers teachers) {
        try {
            teachers.setFlag("1");
            teachersService.saveService(teachers);
            return "redirect:/teacher/";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/mess/")
    public String showTeacherDetails(@RequestParam("tid") Integer tid, Model model) {
        try {
            Teachers query = new Teachers();
            query.setTid(tid);
            Teachers teacher = teachersService.findAllService(query);
            model.addAttribute("t", teacher);
            return "mess";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/update_teacher/")
    public String showUpdateTeacherPage(@RequestParam("tid") Integer tid, Model model) {
        try {
            Teachers query = new Teachers();
            query.setTid(tid);
            Teachers teacher = teachersService.findAllService(query);
            model.addAttribute("t", teacher);
            return "updateteacher";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/child/")
    public String childPage() {
        return "child";
    }

    @GetMapping("/posts/")
    public String postPage() {
        return "posts";
    }

    @GetMapping("/add_posts/")
    public String addPostPage() {
        return "addposts";
    }

    @GetMapping("/update_posts/")
    public String showUpdatePostPage(@RequestParam("pid") Integer pid, Model model) {
        try {
            Post query = new Post();
            query.setPid(pid);
            Post post = postService.findAllService(query);
            model.addAttribute("c", post);
            return "updateposts";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/add_child/")
    public String addChildPage() {
        return "addchild";
    }

    @GetMapping("/update_child")
    public String changeChild(@RequestParam("cid") Integer cid, Model model) {
        try {
            Child query = new Child();
            query.setCid(cid);
            Child child = childService.findChild(query);
            model.addAttribute("c", child);
            return "updatechild";
        } catch (Exception e) {
            return "error";
        }
    }

    @GetMapping("/child_sign")
    public String toChildSignPage(@RequestParam("cid") Integer cid, Model model) {
        model.addAttribute("cid", cid);
        return "childsign";
    }

    @GetMapping("/sgin_in")
    public String childSignIn(@RequestParam("id") Integer cid, Model model) {
        model.addAttribute("cid", cid);
        return "redirect:/child_sign/?cid=" + cid;
    }

    @GetMapping("/teacher_sign")
    public String toTeacherSignPage(@RequestParam("tid") Integer tid, Model model) {
        model.addAttribute("tid", tid);
        return "teachersign";
    }

    @GetMapping("/sign_in_teacher")
    public String teacherSignIn(@RequestParam("id") Integer tid, Model model) {
        model.addAttribute("tid", tid);
        return "redirect:/teacher_sign?tid=" + tid;
    }

    @GetMapping("/child_pay")
    public String toChildPayPage(@RequestParam("cid") Integer cid, Model model) {
        Child child = childService.findById(cid);
        model.addAttribute("cc", child);
        return "childpay";
    }

    @GetMapping("/child_pay/add")
    public String toAddChildPayPage(@RequestParam("cid") Integer cid, Model model) {
        model.addAttribute("cid", cid);
        return "addchildpay";
    }

    @GetMapping("/update_childpay")
    public String toUpdateChildPayPage(@RequestParam("id") Integer cpid,
                                       @RequestParam("cid") Integer cid,
                                       Model model) {
        ChildPay childPay = childPayService.findById(cpid);
        model.addAttribute("childPay", childPay);

        model.addAttribute("cid", cid);

        return "updatechildpay";
    }

    @GetMapping("/teacher_sal/list")
    public String dispatchSalaryPage(@RequestParam("id") Integer tid, @RequestParam("ttype") Integer ttype, Model model) {
        Teachers t = teachersService.findTeaById(tid);
        model.addAttribute("t", t);

        if (ttype == 1) {
            return "teachersal_1";
        } else {
            return "teachersal_2";
        }
    }

    @GetMapping("/teacher_sal/add")
    public String toAddTeacherSalPage(@RequestParam("id") Integer tid,
                                      @RequestParam("sid") Integer ttype,
                                      Model model) {
        Teachers t = teachersService.findTeaById(tid);
        model.addAttribute("t", t);

        if (ttype == 1) {
            return "addteachersal_1";
        } else {
            return "addteachersal_2";
        }
    }

    @GetMapping("/update_teachersal")
    public String toUpdateTeacherSalPage(@RequestParam("sid") Integer sid,
                                         @RequestParam("tid") Integer tid,
                                         @RequestParam("ttype") Integer ttype,
                                         Model model) {
        GSal gsal = gsalService.findById(sid);
        model.addAttribute("gsal", gsal);

        YSal ySal = ySalService.findById(sid);
        model.addAttribute("ySal", ySal);

        Teachers teacher = teachersService.findTeaById(tid);
        model.addAttribute("t", teacher);

        if (ttype == 1) {
            return "updateteachersal_1";
        } else {
            return "updateteachersal_2";
        }
    }

    @GetMapping("/sal_mess")
    public String toSalaryDetailPage(@RequestParam("sid") Integer sid,
                                     @RequestParam("ttype") Integer ttype,
                                     Model model) {
        if (ttype == 1) {
            YSal ySal = ySalService.findById(sid);
            if (ySal == null) {
                model.addAttribute("error", "未找到对应的托育老师工资记录");
            }
            model.addAttribute("ysal", ySal);

            Teachers teacher = teachersService.findTeaById(ySal.getTid());
            model.addAttribute("t", teacher);

            return "salmessy";
        } else {
            GSal gsal = gsalService.findById(sid);
            if (gsal == null) {
                model.addAttribute("error", "未找到对应的托管老师工资记录");
            }
            model.addAttribute("gsal", gsal);

            Teachers teacher = teachersService.findTeaById(gsal.getTid());
            model.addAttribute("t", teacher);

            return "salmessg";
        }
    }



}


